﻿using Prism.Commands;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using SmartPlant.WinCore.Helper;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Windows.Input;
using System.Xml;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class TemplateSettingEditXMLViewModel : DialogViewModelBase, IDialogAware
    {
        private DelegateCommand<object> _okDialogCommand;
        private ITemplateSettingService _templateSettingService;
        public TemplateSettingEditXMLViewModel(IDialogService dialogService, ITemplateSettingService tempServ) : base(dialogService)
        {
            Title = "编辑XML";
            _templateSettingService = tempServ;
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            base.OnDialogOpened(parameters);
            TemplateName = parameters.GetValue<string>("Name");
            InOrOut = parameters.GetValue<int>("InOrOut");
            Type = Enum.Parse<TemplateType>(parameters.GetValue<string>("Type"));
            TemplatePath = parameters.GetValue<string>("TemplatePath");
            XML = parameters.GetValue<string>("XML");
        }
        private int _InOrOut;
        public int InOrOut
        {
            get { return _InOrOut; }
            set { SetProperty(ref _InOrOut, value); }
        }
        private TemplateType _Type;
        public TemplateType Type
        {
            get { return _Type; }
            set { SetProperty(ref _Type, value); }
        }
        private string _templateName = "";
        public string TemplateName
        {
            get { return _templateName; }
            set { SetProperty(ref _templateName, value); }
        }
        private string _XML = "";
        public string XML
        {
            get { return _XML; }
            set { SetProperty(ref _XML, value); }
        }
        private string _templatePath = "";
        public string TemplatePath
        {
            get { return _templatePath; }
            set { SetProperty(ref _templatePath, value); }
        }
        public ICommand OkDialogCommand
        {
            get
            {
                if (_okDialogCommand != null) return _okDialogCommand;
                _okDialogCommand = new DelegateCommand<object>(onOkDialogCommand);
                return _okDialogCommand;
            }
        }
        private async void onOkDialogCommand(object parameter)
        {
            string proId = GlobalObject.CurrentProject.ProjId.ToString();
            if (_XML.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您的输入不规范，XML不能为空。");
                return;
            }
            try
            {
                new XmlDocument().LoadXml(_XML);
            }
            catch (Exception x)
            {
                Dialog.ShowAltert("您的输入XML格式不规范。");
                return;
            }
            try
            {
                using var stream = new MemoryStream();
                using var streamWriter = new StreamWriter(stream, Encoding.UTF8);
                streamWriter.Write(XML);
                streamWriter.Flush();
                stream.Position = 0;
                var r = await _templateSettingService.UploadTemplateFileAsync(proId, Type,stream, TemplateName, InOrOut);
                if (r == true)
                {
                    Dialog.ShowAltert("上传文件至远程服务器成功！！！");
                    RaiseRequestClose(new DialogResult(ButtonResult.OK));
                }
                else
                {
                    RaiseRequestClose(new DialogResult(ButtonResult.None));
                }
            }
            catch (Exception x)
            {
                var xMessage = $"上传文件至远程服务器失败：{x.Message}！！！";
                LoggerHelper.Current.Error(xMessage, x);
                Dialog.ShowAltert(xMessage);
            }
        }
    }
}
